package com.fany.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.EditText;
import android.widget.Toast;
import com.fany.constants.StringResources;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class CommonUtils
{

    public static void showMessage(Context context, String message)
    {
        new AlertDialog.Builder(context).setTitle(StringResources.Hint).setPositiveButton(StringResources.Ok, null).setMessage(message).show();
    }

    public static void showToast(Context context, String message)
    {
        Toast.makeText(context, message, Toast.LENGTH_LONG).show();
    }

    public static void confirm(Context context, String title, String message, DialogInterface.OnClickListener listener)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton(StringResources.Ok, listener);
        builder.setNegativeButton(StringResources.Cancel, new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                dialog.dismiss();
            }
        });
        builder.show();
    }

    public interface InputDialogOKCallback
    {
        public void onOk(String inputResult);
    }

    public interface InputDialogCancelCallback
    {
        public void onCancel();
    }

    public static void inputDialog(Context context, String title, String initContent,
                                   final InputDialogOKCallback okCallback, final InputDialogCancelCallback cancelCallback)
    {
        final EditText edtInput = new EditText(context);
        edtInput.setText(initContent);
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setView(edtInput);
        builder.setPositiveButton(StringResources.Ok, new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                if (okCallback != null)
                {
                    String inputResult = edtInput.getText().toString();
                    okCallback.onOk(inputResult);
                }
                dialog.dismiss();
            }
        });
        builder.setNegativeButton(StringResources.Cancel, new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                if (cancelCallback != null)
                {
                    cancelCallback.onCancel();
                }
                dialog.dismiss();
            }
        });
        builder.show();
    }

    public static String stringToMD5(String string)
    {
        byte[] hash;

        try
        {
            hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
        }
        catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
            return null;
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
            return null;
        }

        StringBuilder hex = new StringBuilder(hash.length * 2);
        for (byte b : hash)
        {
            if ((b & 0xFF) < 0x10)
            {
                hex.append("0");
            }
            hex.append(Integer.toHexString(b & 0xFF));
        }

        return hex.toString();
    }
}
